﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Security.Principal;
using System.Windows.Forms;

namespace SybaseDataResolver
{
    internal static class Program
    {
        static void RunDialog(string[] args)
        {
            args = new string[] { "--dialog" };
            ProcessStartInfo processStartInfo = new ProcessStartInfo();
            processStartInfo.FileName = Application.ExecutablePath;
            processStartInfo.Arguments = string.Join(" ", args);
            processStartInfo.Verb = "runas";
            Process process3 = Process.Start(processStartInfo);
            Application.Exit();
        }
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            
            if(args.Length == 0)
            {
                WindowsIdentity current = WindowsIdentity.GetCurrent();
                Application.EnableVisualStyles();
                WindowsPrincipal windowsPrincipal = new WindowsPrincipal(current);
                if(windowsPrincipal.IsInRole(WindowsBuiltInRole.Administrator))
                {
                    ExtractFile();
                    AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new Form1());
                }
                else{
                    RunDialog(args);
                }
            }
            else{
                ExtractFile();
                AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
        }

        static void ExtractFile(){
            if(!System.IO.Directory.Exists(Application.StartupPath + "\\win32\\dbeng7.exe"))
            {
                FileUtil.ExtractResFile("SybaseDataResolver.resc.win32.zip",Application.StartupPath + "\\win32.zip");
                string zipfile = Application.StartupPath + "\\win32.zip";
                using(var unzip = new Unzip(zipfile)){
                    try
                    {
                        unzip.ExtractToDirectory(Application.StartupPath);
                    }
                    catch(Exception ex)
                    {
                        MessageBox.Show("解压出错=>" + ex.ToString());
                    }
                    finally{
                        try{
                            unzip.Dispose();
                            new FileInfo(zipfile).Delete();
                        }
                        catch(Exception ex){
                            MessageBox.Show("删除出错出错=>" + ex.ToString());
                        }
                    }
                }
            }
        }

        private static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
        {
            string resourceName = "SybaseDataResolver.dlls." + new AssemblyName(args.Name).Name + ".dll";
            
            using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
            {
                Console.WriteLine("resourceName=>" + resourceName);
                Byte[] assemblyData = new Byte[stream.Length];
                stream.Read(assemblyData, 0, assemblyData.Length);
                return Assembly.Load(assemblyData);
            }
        }
    }
}
